<?php
namespace Common\Library;

/**
 * Description of http://www.51shucheng.net/
 *
 * @author gangguo
 */
class wuyishucheng{

    protected $BookArticle;
    protected $detailUrl;
    protected $id;
    protected $book_id;
    protected $author_id;
    protected $authorName;
    protected $bookName;

    public function __construct($id,$url = '',$type = ''){
        header("Content-Type:text/html;charset=utf-8;");
        $this->id = $id;
        $this->detailUrl = $url;
        if(empty($type)){
            $this->addNewBook();
        }elseif($type == 'authorlist'){
            $this->authorlist();
        }elseif($type == 'booklist'){
            $this->booklist();
        }
        exit();
    }

    //添加一本新书x
    protected function addNewBook(){
        if(empty($this->detailUrl)){
            exit('空URL地址');
        }
        set_time_limit(0);
        $pathFile = './Runtime/Collect/51shucheng/addbook.html';
        \Common\Library\COM\CollectDo::downPage($this->detailUrl,$pathFile);
        $getStr = \Common\Library\COM\CollectDo::getHtml($pathFile,'','utf-8');
        $bookInfo = \Common\Library\COM\CollectDo::getSubstr($getStr,'<div class="cse-search">','<div class="ad-');
        $listInfo = \Common\Library\COM\CollectDo::getSubstr($getStr,'<ul>','</ul>');
        unset($getStr,$pathFile,$this->authorName,$this->author_id);
        if(mb_strpos($bookInfo, '作者：') && empty($this->authorName)){
            //提取作者
            $this->authorName = \Common\Library\COM\CollectDo::getSubstr($bookInfo,'作者：','。',FALSE);
            if(empty($this->authorName) || mb_strlen($this->authorName) > 35){
                $this->authorName = \Common\Library\COM\CollectDo::getSubstr($bookInfo,'作者：','<b>内容简介',FALSE);
            }
            $this->authorName = trim(strip_tags($this->authorName));
            if(empty($this->authorName)){
                $this->authorName = '佚名';//补充作者
                $this->writeLog('作者为空!:'.$this->detailUrl.'@@'.var_export($bookInfo,1));
            }
            //作者处理
            $this->author_id = D('Common/BookAuthor')->getAuthorId($this->authorName);

            if(empty($this->author_id)){
                $this->writeLog('作者ID错误:'.$this->author_id.' @@ '.$this->authorName.' @@ '.$this->detailUrl,'ERR');
            }
        }elseif(empty($this->authorName)){
            $this->author_id = 429;// 佚名
        }
        //提取书籍名称
        $this->bookName = \Common\Library\COM\CollectDo::getSubstr($bookInfo,'<h1>','</h1>',FALSE);
        if(empty($this->bookName)){
            $this->writeLog('书名为空!:'.$this->detailUrl.'@@'.var_export($bookInfo,1),'ERR');
        }

        $Book = new \Common\Model\BookModel();
        $unique = md5($this->bookName.$this->author_id);
        //书本重复检测
        $this->book_id = $Book->checkBookRepeart($unique);
        //p($book_id);exit;
        if(empty($this->book_id)){//新书入库
            //提取简介
            $intro = \Common\Library\COM\CollectDo::getSubstr($bookInfo,'<div style="text-indent:2em">','',FALSE);
            $intro = \Common\Library\COM\CollectDo::getSubstr($intro,'','</div>',FALSE);
            $intro = strip_tags($intro);
            //提取分类
            $cate = \Common\Library\COM\CollectDo::getSubstr($bookInfo,'所属类目：','</a>',FALSE);
            $cate = strip_tags($cate);
            $cate = \Common\Library\COM\CollectDo::changeCateName($cate);
            if(empty($cate)){
                $this->writeLog('入库错误,没有分类:'.var_export($cate,1).'@@'.var_export($bookInfo,1),'ERR');
            }
            //提取封面图片
            $image = '';
            //分类处理
            $cate_id = D('Common/BookCate')->getCateId($cate);
            //书籍状态
            $status = 'end';
            $dataBook = array(
                'book_name'=>$this->bookName,
                'author_id'=>$this->author_id,
                'status'=>$status,
                'cate_id'=>$cate_id,
                'introduction'=> htmlentities($intro,ENT_COMPAT,'UTF-8'),
                'img'=>$image,
                'create_time'=>  time(),
                'year'=> date('Y'),
                'month'=>  date('m'),
                'day'=>  date('d'),
                'unique'=>$unique
            );
            //p($dataBook);exit;
            $this->book_id = $Book->saveBook($dataBook);
        }
        //新书开始入库
        if(empty($this->book_id)){
            $this->writeLog('入库错误,书本未入库:'.var_export($dataBook,1).'@@'.var_export($Book->getDbError(),1),'ERR');
        }else{
            $this->writeLog('bookinstall.新书开始入库:'.var_export($this->bookName,1));
        }

        $this->articleCollection($listInfo);
        unset($bookInfo,$Book,$listInfo);
    }

    //章节列表名称入库
    protected function articleCollection($listInfo=''){
        if(empty($listInfo) || empty($this->book_id)){ return FALSE;}
        //章节名称和篇名入库
        $count = mb_substr_count($listInfo,'<li><a');
        $offset = 0;
        $this->BookArticle = new \Common\Model\BookArticleModel();
        for($i=0;$i<$count;$i++){
            $nowInfo = \Common\Library\COM\CollectDo::getSubstr($listInfo,'','</li>',FALSE);
            //获取章节名称
            $article_name = \Common\Library\COM\CollectDo::getSubstr($nowInfo,'">','</a>',FALSE);

            if(empty($article_name)){
                $this->writeLog('章节名称不能为空!@Value:'.var_export($nowInfo,1).'@URL:'.$this->detailUrl,'ERR');
            }
            $this->writeLog($article_name.':章节入库开始!');
            $unique = md5($this->book_id.$article_name);
            $result = $this->BookArticle->checkArtcileRepeat($unique);
            if($result){//重复章节检测
                $this->writeLog($article_name.' ====> 重复章节');
            }else{//采集新章节
                //获取章节内容url地址
                $article_url = \Common\Library\COM\CollectDo::getSubstr($nowInfo,'<a href="','" title="',FALSE);
                //获取章节内容并存储
                $contentPath = $this->contentCollection($article_url);
                $article_num = \Common\Library\COM\CollectDo::getArticleNum($article_name);
                $articleDate = array(
                    'book_id'=> $this->book_id,
                    'article_name'=> $article_name,
                    'create_time'=>  time(),
                    'year'=>  date('Y'),
                    'month'=>  date('m'),
                    'day'=>  date('d'),
                    'article_num'=> $article_num,
                    'update_time' => time(),
                    'unique'=>$unique,
                    'content'=>$contentPath
                );
                //p($article_url);exit;
                $article_id = $this->BookArticle->add($articleDate);
                if(empty($article_id)){
                    $this->writeLog('章节存储失败!@'.var_export($articleDate,1).var_export($this->BookArticle->getDbError(),1),'ERR');
                }
            }
            $listInfo =  \Common\Library\COM\CollectDo::getSubstr($listInfo,'</li>','',FALSE);
        }
        $this->writeLog($this->detailUrl.'本书入库完成!');
        return FALSE;
    }

    //章节内容入库
    protected function contentCollection($contentUrl = ''){
        if(empty($contentUrl)){
            $this->writeLog('内容章节URL地址空:'.var_export($contentUrl,1),'ERR');
            exit;
        }
        $savePath = './Runtime/Collect/51shucheng/content.html';
        \Common\Library\COM\CollectDo::downPage($contentUrl, $savePath);
        $contentA = \Common\Library\COM\CollectDo::getHtml($savePath,'','utf-8');
        if(empty($contentA)){
            \Common\Library\COM\CollectDo::downPage($contentUrl, $savePath);
            //sleep(3);
            $contentA = \Common\Library\COM\CollectDo::getHtml($savePath);
        }
        $contentA = \Common\Library\COM\CollectDo::getSubstr($contentA,'<div class="neirong">','<div class="book-nav">',FALSE);
        $contentA = \Common\Library\COM\CollectDo::getSubstr($contentA,'','</div>',FALSE);

        $replace = array(
                '</div>','<a href="','<a href=','<a','</a>',
                'www.51shucheng.com','51shucheng.com','无忧书城');
        $contentT = str_replace($replace,'', $contentA);unset($contentA);

        if(!\Common\Library\COM\CollectDo::checkSpecialStr($contentT)){
            $data = array('id'=>$this->id,'dotime'=>time(),'disabled'=>4,'remark'=>'采集章节内容有误,包含特殊字符:'.$contentUrl);
            \HelloAdmin\Model\CrontabModel::updateData($data);
            $this->writeLog('采集章节内容有误,包含特殊字符:'.var_export($contentUrl,1).'@@'.var_export($contentT,1),'ERR');
        }
        $contentT = htmlentities($contentT,ENT_COMPAT,'UTF-8');
        $contentPath = \Common\Library\COM\CollectDo::doContentPath();
        $handel = fopen('.'.$contentPath,'x+');
        if($handel && fwrite($handel,$contentT) !== FALSE){
            fclose($handel);
            return $contentPath;
        }
        $this->writeLog('章节内容'.var_export($contentT,1));
        $this->writeLog('章节内容存储失败!@'.var_export($contentPath,1).'|@|'.var_export($contentUrl,1),'ERR');
    }

    //采集日志记录
    private function writeLog($message,$level='INFO'){
        \Think\Log::write($message,$level,$type='','./Runtime/Collect/51shucheng/logs-'.  date('Ymd').'.log');
        if($level == 'ERR'){
            exit($message);
        }
    }

    public function authorlist(){
        set_time_limit(0);
        $pathFile = './Runtime/Collect/51shucheng/authorlist.html';
        \Common\Library\COM\CollectDo::downPage($this->detailUrl, $pathFile);
        $getStr = \Common\Library\COM\CollectDo::getHtml($pathFile,'','utf-8');
        $authorInfo = \Common\Library\COM\CollectDo::getSubstr($getStr,'class="zuojia-pic">','<table width="800"');
        $booklist = \Common\Library\COM\CollectDo::getSubstr($getStr,'</tr>','</table>');
        unset($getStr,$pathFile);
        if(strpos($authorInfo,'作品集"') > 0){
            $this->authorName = \Common\Library\COM\CollectDo::getSubstr($authorInfo,'<img title="','作品集"');
        }elseif(strpos($authorInfo,'武侠小说全集')){
            $this->authorName = \Common\Library\COM\CollectDo::getSubstr($authorInfo,'<img title="','武侠小说全集');
        }else{
            exit('没有作者'.$this->detailUrl);
        }
        $Author = new \Common\Model\BookAuthorModel();
        $this->author_id = $Author->getAuthorId($this->authorName);
        $auInfo = $Author->where("`author_id`='{$this->author_id}'")->find();
        $data = null;
        if(empty($auInfo['img'])){
            $au_img = \Common\Library\COM\CollectDo::getSubstr($authorInfo,' src="','" alt="');
            $data['img'] = \Common\Library\COM\CollectDo::doImagePath();
            \Common\Library\COM\CollectDo::downPage($au_img,$au_img_path);
        }
        if(empty($auInfo['intro'])){
            $au_intro = \Common\Library\COM\CollectDo::getSubstr($authorInfo,'style="text-indent: 2em;">','',FALSE);
            $data['intro'] = strip_tags($au_intro);
        }
        if(!empty($data)){
            $Author->where("`author_id`='{$this->author_id}'")->save($data);
        }
        //获取书籍名称
        $duanbook = \Common\Library\COM\CollectDo::getSubstr($booklist,'<td><a href="','</tbody>',FALSE);
        $count = mb_substr_count($booklist,'<td colspan="');
        for($i=0;$i<$count;$i++){// 书籍采集
            $nowInfo = \Common\Library\COM\CollectDo::getSubstr($booklist,'','</tr>',FALSE);
            $bookUrl = \Common\Library\COM\CollectDo::getSubstr($nowInfo,'<h2><a href="','" target="_blank">',FALSE);
            $this->detailUrl = $bookUrl;
            //p($bookUrl);echo '<hr>';
            $this->addNewBook();
            $booklist = \Common\Library\COM\CollectDo::getSubstr($booklist,'</tr>','',FALSE);
            //sleep(30);
        }
        unset($nowInfo,$count,$booklist,$bookUrl);
        $count = mb_substr_count($duanbook,'<td><a href="');
        $this->BookArticle = new \Common\Model\BookArticleModel();
        for($i=0;$i<$count;$i++){//短篇小说和散文采集
            $nowInfo = \Common\Library\COM\CollectDo::getSubstr($duanbook,'','<a href="',FALSE);
            $bookUrl = \Common\Library\COM\CollectDo::getSubstr($nowInfo,'','" title="',FALSE);
            $this->detailUrl = $bookUrl;
            //p($bookUrl);echo '<hr>';
            $this->addOneArticle();
            $duanbook = \Common\Library\COM\CollectDo::getSubstr($duanbook,'<a href="','',FALSE);
            //sleep(30);
        }
    }

    //单篇文章和小说的采集
    public function addOneArticle(){
        if(empty($this->detailUrl)){
            $this->writeLog('采集地址为空','ERR');
        }
        set_time_limit(0);
        $pathFile = './Runtime/Collect/51shucheng/oneaddbook.html';
        \Common\Library\COM\CollectDo::downPage($this->detailUrl,$pathFile);
        $getStr = \Common\Library\COM\CollectDo::getHtml($pathFile,'','utf-8');
        if(mb_strpos($getStr,'<div class="mulu-list">')){
            $this->addNewBook();
            return FALSE;
        }

        $bookInfo = \Common\Library\COM\CollectDo::getSubstr($getStr,'<div class="crumb">','<div class="ad-',FALSE);
        unset($getStr,$pathFile,$bookI);
        //p($bookI);p($bookInfo);p($content);exit;
        if(empty($this->author_id)){
            $this->writeLog('作者ID错误:'.$this->author_id.' @@ '.$this->authorName.' @@ '.$this->detailUrl,'ERR');
        }

        //提取书籍名称
        $this->bookName = \Common\Library\COM\CollectDo::getSubstr($bookInfo,'<h1>','</h1>',FALSE);
        if(empty($this->bookName)){
            $this->writeLog('书名为空!:'.$this->detailUrl.'@@'.var_export($bookInfo,1),'ERR');
        }
        $Book = new \Common\Model\BookModel();
        $unique = md5($this->bookName.$this->author_id);
        //书本重复检测
        $this->book_id = $Book->checkBookRepeart($unique);
        //p($this->book_id);exit;
        if(empty($this->book_id)){//新书入库
            //提取简介
            $intro = '';
            //提取分类
            $cate = \Common\Library\COM\CollectDo::getSubstr($bookInfo,'category">','',FALSE);
            $cate = \Common\Library\COM\CollectDo::getSubstr($cate,'','</a> &gt; <a title="',FALSE);
            $cate = \Common\Library\COM\CollectDo::changeCateName($cate);
            if(empty($cate)){
                $this->writeLog('没有分类:'.var_export($cate,1).var_export($bookInfo,1),'ERR');
            }
            //提取封面图片
            $image = '';
            //分类处理
            $cate_id = D('Common/BookCate')->getCateId($cate);
            //书籍状态
            $status = 'end';
            $dataBook = array(
                'book_name'=>$this->bookName,
                'author_id'=>$this->author_id,
                'status'=>$status,
                'cate_id'=>$cate_id,
                'introduction'=> htmlentities($intro,ENT_COMPAT,'UTF-8'),
                'img'=>$image,
                'create_time'=>  time(),
                'year'=> date('Y'),
                'month'=>  date('m'),
                'day'=>  date('d'),
                'unique'=>$unique
            );
            //p($dataBook);exit;
            $this->book_id = $Book->saveBook($dataBook);
        }
        //新书开始入库
        if(empty($this->book_id)){
            $this->writeLog('入库错误,书本未入库:'.var_export($dataBook,1).'@@'.var_export($Book->getDbError(),1),'ERR');
        }else{
            $this->writeLog('bookinstall.新书开始入库:'.var_export($this->bookName,1));
            //获取章节内容并存储
            $contentPath = $this->contentCollection($this->detailUrl);
            $articleDate = array(
                'book_id'=> $this->book_id,
                'article_name'=> $this->bookName,
                'create_time'=>  time(),
                'year'=>  date('Y'),
                'month'=>  date('m'),
                'day'=>  date('d'),
                'article_num'=> 1,
                'update_time' => time(),
                'unique'=>md5($this->book_id.$this->bookName),
                'content'=>$contentPath
            );
            //p($articleDate);exit;
            $article_id = $this->BookArticle->add($articleDate);
            if(empty($article_id)){
                $this->writeLog('章节存储失败!@'.var_export($articleDate,1).var_export($this->BookArticle->getDbError(),1),'ERR');
            }
        }
    }

    public function booklist(){
        set_time_limit(0);
        $pathFile = './Runtime/Collect/51shucheng/booklist.html';
        \Common\Library\COM\CollectDo::downPage($this->detailUrl, $pathFile);
        $getStr = \Common\Library\COM\CollectDo::getHtml($pathFile,'','utf-8');
        $booklist = \Common\Library\COM\CollectDo::getSubstr($getStr,'<ul>','</ul>',FALSE);
        unset($getStr,$pathFile);

        //获取书籍名称
        $count = mb_substr_count($booklist,'<li class="cat');
        $Book = new \Common\Model\BookModel();
        for($i=0;$i<$count;$i++){
            $nowInfo = \Common\Library\COM\CollectDo::getSubstr($booklist,'','</li>',FALSE);
            $bookUrl = \Common\Library\COM\CollectDo::getSubstr($nowInfo,'<a href="','" >',FALSE);
            $this->detailUrl = $bookUrl;
            //p($bookUrl);p($nowInfo);echo '<hr>';
            $this->addNewBook();
            $booklist = \Common\Library\COM\CollectDo::getSubstr($booklist,'</li>','',FALSE);
            //sleep(30);
        }
    }

}
